package com.css.common.core.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtil {
    /**
     * 导出
     * @param response
     * @param data
     * @param fileName
     * @param sheetName
     * @param clazz
     * @throws Exception
     */
    public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {
        //表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置内容靠左对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);

        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(getExcelOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerConverter(new LongStringConverter()).registerWriteHandler(horizontalCellStyleStrategy).doWrite(data);
    }

    /**
     * 导出 俩个sheet页
     * @param response
     * @param data1
     * @param fileName
     * @param sheetName1
     * @param clazz1
     * @throws Exception
     */
    public static void writeExcel2Sheet(HttpServletResponse response, List<? extends Object> data1, List<? extends Object> data2, String fileName, String sheetName1, String sheetName2, Class clazz1, Class clazz2) throws Exception {
        //表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置内容靠左对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        ExcelWriter excelWriter = EasyExcel.write(getExcelOutputStream(fileName, response)).registerWriteHandler(horizontalCellStyleStrategy).build();
        WriteTable writeTable = EasyExcel.writerTable(0).needHead(Boolean.TRUE).head(clazz1).build();
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetNo(1);
        writeSheet.setSheetName(sheetName1);
        excelWriter.write(data1, writeSheet, writeTable);
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1,sheetName2).head(clazz2).build();
        writeSheet2.setSheetNo(2);
        writeSheet2.setSheetName(sheetName2);
        excelWriter.write(data2,writeSheet2);
        excelWriter.finish();
    }

    /**
     * 导出 三个sheet页
     * @param response
     * @param data1
     * @param fileName
     * @param sheetName1
     * @param clazz1
     * @throws Exception
     */
    public static void writeExcel3Sheet(HttpServletResponse response, List<? extends Object> data1, List<? extends Object> data2 , List<? extends Object> data3 , String fileName, String sheetName1, String sheetName2 , String sheetName3, Class clazz1, Class clazz2 , Class clazz3) throws Exception {
        //表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置内容靠左对齐
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        ExcelWriter excelWriter = EasyExcel.write(getExcelOutputStream(fileName, response)).registerWriteHandler(horizontalCellStyleStrategy).build();
        WriteTable writeTable = EasyExcel.writerTable(0).needHead(Boolean.TRUE).head(clazz1).build();
        WriteSheet writeSheet = new WriteSheet();
        writeSheet.setSheetNo(1);
        writeSheet.setSheetName(sheetName1);
        excelWriter.write(data1, writeSheet, writeTable);
        WriteSheet writeSheet2 = EasyExcel.writerSheet(1,sheetName2).head(clazz2).build();
        writeSheet2.setSheetNo(2);
        writeSheet2.setSheetName(sheetName2);
        excelWriter.write(data2,writeSheet2);
        WriteSheet writeSheet3 = EasyExcel.writerSheet(1,sheetName3).head(clazz3).build();
        writeSheet3.setSheetNo(3);
        writeSheet3.setSheetName(sheetName3);
        excelWriter.write(data3,writeSheet3);
        excelWriter.finish();
    }
    
    public static OutputStream getExcelOutputStream(String fileName, HttpServletResponse response) throws Exception {
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.setCharacterEncoding("utf8");
        response.setHeader("content-type","application/octet-stream;charset=ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        return response.getOutputStream();
    }
    
}
